/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.emove.modular.system.controller;

import cn.emove.core.common.annotion.BussinessLog;
import cn.emove.core.common.annotion.Permission;
import cn.emove.core.common.constant.Const;
import cn.emove.core.common.constant.dictmap.DictMap;
import cn.emove.core.common.constant.factory.ConstantFactory;
import cn.emove.core.common.exception.BizExceptionEnum;
import cn.emove.core.common.page.LayuiPageFactory;
import cn.emove.core.log.LogObjectHolder;
import cn.emove.core.util.HttpUtil;
import cn.emove.modular.system.model.DictDto;
import cn.emove.modular.system.service.DictService;
import cn.emove.modular.system.warpper.DictWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 字典控制器
 *
 * @author fengshuonan
 * @Date 2017年4月26日 12:55:31
 */
@Controller
@RequestMapping("/runbatch")
public class RunBatchController extends BaseController {

    private String PREFIX = "/modular/system/runbatch/";

    @Autowired
    private DictService dictService;

    /**
     * 跳转到字典管理首页
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:21 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "runbatch.html";
    }


    /**
     * 发送通知
     * @return
     */

    @RequestMapping(value = "/purchaseComplete")
    @ResponseBody
    public ResponseData purchaseComplete() {
        String ip = dictService.selectByCode("service_intranet_ip").get(0).getDescription();
        String url = ip+"/api/material/getPurchaseComplete";
        String result = HttpUtil.sendGet(url);
        System.out.println(result);
        if(result!=""){
            JSONObject json = JSONObject.parseObject(result);
            String code = String.valueOf(json.get("code"));
            if("200".equals(code)){
                return SUCCESS_TIP;
            }else{
                throw new ServiceException(BizExceptionEnum.SERVER_ERROR);
            }
        }else {
            throw new ServiceException(BizExceptionEnum.SESSION_TIMEOUT);
        }
    }

    /**
     * 同步用户信息跑批
     * @return
     */
    @RequestMapping(value = "/synchronizedUserData")
    @ResponseBody
    public ResponseData synchronizedUserData() {
        String ip = dictService.selectByCode("service_intranet_ip").get(0).getDescription();
        String url = ip+"/api/user/synchronizedUserData";
        String result = HttpUtil.sendGet(url);
        System.out.println(result);
        if(result!=""){
            JSONObject json = JSONObject.parseObject(result);
            String code = String.valueOf(json.get("code"));
            String message = String.valueOf(json.get("message"));
            if("200".equals(code)){
                if("无新数据可更新".equals(message)){
                    throw new ServiceException(BizExceptionEnum.RUN_BATCH_ERROR);
                }
                return SUCCESS_TIP;
            }else{
                throw new ServiceException(BizExceptionEnum.SERVER_ERROR);
            }
        }else {
            throw new ServiceException(BizExceptionEnum.SESSION_TIMEOUT);
        }
    }

    /**
     * 同步主物料信息跑批
     * @return
     */
    @RequestMapping(value = "/synchronizedMaterialData")
    @ResponseBody
    public ResponseData synchronizedMaterialData() {
        String ip = dictService.selectByCode("service_intranet_ip").get(0).getDescription();
        String url = ip+"/api/material/getMainMaterial";
        String result = HttpUtil.sendGet(url);
        System.out.println(result);
        if(result!=""){
            JSONObject json = JSONObject.parseObject(result);
            String code = String.valueOf(json.get("code"));
            String message = String.valueOf(json.get("message"));
            if("200".equals(code)){
                if("无新数据可更新".equals(message)){
                    throw new ServiceException(BizExceptionEnum.RUN_BATCH_ERROR);
                }
                return SUCCESS_TIP;
            }else{
                throw new ServiceException(BizExceptionEnum.SERVER_ERROR);
            }
        }else {
            throw new ServiceException(BizExceptionEnum.SESSION_TIMEOUT);
        }
    }

    /**
     * 获取当月抄表信息跑批
     * @return
     */
    @RequestMapping(value = "/getMeterReadingData")
    @ResponseBody
    public ResponseData getMeterReadingData() {
        String ip = dictService.selectByCode("service_intranet_ip").get(0).getDescription();
        String url = ip+"/api/payment/getMeterReadingData";
        String result = HttpUtil.sendGet(url);
        System.out.println(result);
        if(result!=""){
            JSONObject json = JSONObject.parseObject(result);
            String code = String.valueOf(json.get("code"));
            String message = String.valueOf(json.get("message"));
            if("200".equals(code)){
                if("无新数据可更新".equals(message)){
                    throw new ServiceException(BizExceptionEnum.RUN_BATCH_ERROR);
                }
                return SUCCESS_TIP;
            }else{
                throw new ServiceException(BizExceptionEnum.SERVER_ERROR);
            }
        }else {
            throw new ServiceException(BizExceptionEnum.SESSION_TIMEOUT);
        }
    }


  /*  *//**
     * 获取当月抄表信息跑批
     * @return
     *//*
    @RequestMapping(value = "/getMeterModelData")
    @ResponseBody
    public ResponseData getMeterModelData() {
        String ip = dictService.selectByCode("service_intranet_ip").get(0).getDescription();
        String url = "http://localhost:8008/api/payment/getMeterReadingData?meterModelType=2";
        String result = HttpUtil.sendGet(url);
        System.out.println(result);
        if(result!=""){
            JSONObject json = JSONObject.parseObject(result);
            String code = String.valueOf(json.get("code"));
            String message = String.valueOf(json.get("message"));
            if("200".equals(code)){
                if("无新数据可更新".equals(message)){
                    throw new ServiceException(BizExceptionEnum.RUN_BATCH_ERROR);
                }
                return SUCCESS_TIP;
            }else{
                throw new ServiceException(BizExceptionEnum.SERVER_ERROR);
            }
        }else {
            throw new ServiceException(BizExceptionEnum.SESSION_TIMEOUT);
        }
    }*/
}
